package com.datechange.demo.webservice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.datechange.demo.controller.Test;
import com.datechange.demo.entity.CmsAttachment;
import com.datechange.demo.entity.ContentObject;
import com.datechange.demo.entity.UploadFileBack;
import com.datechange.demo.service.CmsAttachmentService;
import com.datechange.demo.service.CmsPublicContentService;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;
import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.net.URL;
import java.util.List;

/**
 * @author XY
 * @version 1.0
 * @date 2020/12/14 15:58
 * @Description
 */
@Component
public class MyWebService {

    @Autowired
    CmsAttachmentService cmsAttachmentService;

    @Autowired
    CmsPublicContentService cmsPublicContentService;

    public void putFileToChannelAndUpdate(List<CmsAttachment> attachs, String channel){
        try{
            for (CmsAttachment cmsAttachment : attachs) {
                //将附件下载下来，并获取到下载后的路径
                String filepath = Test.downLoadJpg(cmsAttachment.getCaId());
//                String filepath = "D:\\test\\1.txt";
                String endPoint = "http://192.168.0.253:8080/cms/services/FileService";
                Service service = new Service();
                Call call = (Call) service.createCall();
                QName qn = new QName(endPoint, "DataHandler");
                call.setTargetEndpointAddress(new URL(endPoint));
                call.registerTypeMapping(DataHandler.class, qn,
                        JAFDataHandlerSerializerFactory.class,
                        JAFDataHandlerDeserializerFactory.class);
                DataHandler dataHandler = new DataHandler(new FileDataSource(
                        filepath));
                //指定方法的命名空间
                call.setOperationName(new QName(endPoint, "putFileToChannel"));
                call.addParameter("s1", qn, ParameterMode.IN);
                call.addParameter("s2", XMLType.XSD_STRING, ParameterMode.IN);
                call.addParameter("s3", XMLType.XSD_STRING, ParameterMode.IN);
                call.setReturnType(XMLType.XSD_STRING);
                String result = (String) call.invoke(new Object[] { dataHandler,
                        filepath, channel });
                //同步附件后返回的数据
                UploadFileBack uploadFileBack = JSONObject.parseObject(result, UploadFileBack.class);
                //回写新文件名和路径
                cmsAttachment.setNewFileName(uploadFileBack.getMsg().substring(uploadFileBack.getMsg().lastIndexOf("/")));
                cmsAttachment.setNewFilePath(uploadFileBack.getMsg());
                cmsAttachmentService.update(cmsAttachment);
                System.out.println("附件上传成功："+result);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void doinfo(ContentObject contentObject){
        try {
            String endpoint="http://192.168.0.253:8080/cms/services/ContentService";
            Service service = new Service();
            Call call =(Call) service.createCall();
            QName qn = new QName("urn:ContentService", "ContentObject");
            call.registerTypeMapping(
                    ContentObject.class, qn,
                    new BeanSerializerFactory(ContentObject.class, qn),
                    new BeanDeserializerFactory(ContentObject.class, qn));
            call.setTargetEndpointAddress(new java.net.URL(endpoint));
            call.setOperationName(new QName("ContentService", "insertContent"));
            call.addParameter("caform", qn, ParameterMode.IN);
            call.setReturnType(XMLType.XSD_INT);
            Integer retl=(Integer) call.invoke(new Object[]{contentObject});
            System.out.println("内容上传成功："+retl);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
